package model;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import view.MainWindow;

/**
 * Create a new text file with the contact modification updated for one contact
 * @author Marie Morin
 *
 */
public class ModifyModel {

		
		private Contact c;
		private MainWindow w;
		private ContactListModel contactL = new ContactListModel();
		ArrayList<Contact> contactsList;
		
		public ModifyModel (Contact c, MainWindow w){
			
			this.c = c;
			this.w = w;
			
			this.contactsList = contactL.getContactsList();
			
			int contact=-1;
			
			if (contactsList.size()>0){
				
				for(int i=0; i<contactsList.size(); i++){
					
			        if (equal(contactsList.get(i), c)){
			        	
			        	contact = i;
			        }
			        System.out.println(equal(contactsList.get(i), c));
			    }
				
					if (contact != -1){
						
						contactsList.remove(contact);
					}
				
			}

			
			try {
				this.deleteFile();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
		}
		
		public boolean equal(Contact a, Contact b){
			
			boolean test = false;
			
			if (a.getAname().equals(b.getAname())){
				test = true;
			}
			
			return test;
		}
		
		
		/**
		 * Create a new text file with the contact modification updated for one contact
		 */
		public void deleteFile() throws FileNotFoundException, UnsupportedEncodingException{
			
			PrintWriter writer = new PrintWriter("test.txt", "UTF-8");
			
			if (contactsList.size()>0){
				
				for(int i=0; i<contactsList.size()-1; i++){
					
					writer.println(contactsList.get(i).getName());
					writer.println(contactsList.get(i).getFname());
					writer.println(contactsList.get(i).getPhone());
					writer.println(contactsList.get(i).getEmail());
					writer.println(contactsList.get(i).getWebsite());
					writer.println(contactsList.get(i).getHomeAddress());
					writer.println(contactsList.get(i).getProfessionalPhone());
					writer.println(contactsList.get(i).getProfessionalAddress());
					writer.println(contactsList.get(i).getGroup());
					writer.println(contactsList.get(i).getDate());
					
				}
				
				writer.println(contactsList.get(contactsList.size()-1).getName());
				writer.println(contactsList.get(contactsList.size()-1).getFname());
				writer.println(contactsList.get(contactsList.size()-1).getPhone());
				writer.println(contactsList.get(contactsList.size()-1).getEmail());
				writer.println(contactsList.get(contactsList.size()-1).getWebsite());
				writer.println(contactsList.get(contactsList.size()-1).getHomeAddress());
				writer.println(contactsList.get(contactsList.size()-1).getProfessionalPhone());
				writer.println(contactsList.get(contactsList.size()-1).getProfessionalAddress());
				writer.print(contactsList.get(contactsList.size()-1).getGroup());
				writer.print(contactsList.get(contactsList.size()-1).getDate());

				writer.close();		
				
			}
					
			
		}


}
